import type { User } from '../types';

// 模拟用户数据
export const mockUsers: User[] = [
  {
    id: '1',
    username: 'admin',
    email: 'admin@example.com',
    role: 'admin',
    status: 'active',
    createdAt: '2024-01-01T00:00:00Z',
    updatedAt: '2024-01-01T00:00:00Z',
  },
  {
    id: '2',
    username: 'user1',
    email: 'user1@example.com',
    role: 'user',
    status: 'active',
    createdAt: '2024-01-02T00:00:00Z',
    updatedAt: '2024-01-02T00:00:00Z',
  },
  {
    id: '3',
    username: 'user2',
    email: 'user2@example.com',
    role: 'user',
    status: 'inactive',
    createdAt: '2024-01-03T00:00:00Z',
    updatedAt: '2024-01-03T00:00:00Z',
  },
  {
    id: '4',
    username: 'manager',
    email: 'manager@example.com',
    role: 'admin',
    status: 'active',
    createdAt: '2024-01-04T00:00:00Z',
    updatedAt: '2024-01-04T00:00:00Z',
  },
  {
    id: '5',
    username: 'testuser',
    email: 'test@example.com',
    role: 'user',
    status: 'active',
    createdAt: '2024-01-05T00:00:00Z',
    updatedAt: '2024-01-05T00:00:00Z',
  },
];

// 模拟登录验证
export const mockLogin = (username: string, password: string): Promise<{ success: boolean; user?: User; message?: string }> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 简单的登录验证逻辑
      if (username === 'admin' && password === 'admin123') {
        const user = mockUsers.find(u => u.username === username);
        resolve({ success: true, user });
      } else if (username === 'user1' && password === 'user123') {
        const user = mockUsers.find(u => u.username === username);
        resolve({ success: true, user });
      } else {
        resolve({ success: false, message: '用户名或密码错误' });
      }
    }, 1000);
  });
};

// 模拟用户CRUD操作
export const mockUserAPI = {
  // 获取用户列表
  getUsers: (params: { current: number; pageSize: number; keyword?: string; role?: string; status?: string }) => {
    return new Promise<{ data: User[]; total: number }>((resolve) => {
      setTimeout(() => {
        let filteredUsers = [...mockUsers];
        
        // 搜索过滤
        if (params.keyword) {
          filteredUsers = filteredUsers.filter(user => 
            user.username.toLowerCase().includes(params.keyword!.toLowerCase()) ||
            user.email.toLowerCase().includes(params.keyword!.toLowerCase())
          );
        }
        
        // 角色过滤
        if (params.role) {
          filteredUsers = filteredUsers.filter(user => user.role === params.role);
        }
        
        // 状态过滤
        if (params.status) {
          filteredUsers = filteredUsers.filter(user => user.status === params.status);
        }
        
        const start = (params.current - 1) * params.pageSize;
        const end = start + params.pageSize;
        const data = filteredUsers.slice(start, end);
        
        resolve({ data, total: filteredUsers.length });
      }, 500);
    });
  },

  // 创建用户
  createUser: (userData: Omit<User, 'id' | 'createdAt' | 'updatedAt'>) => {
    return new Promise<{ success: boolean; user?: User; message?: string }>((resolve) => {
      setTimeout(() => {
        const newUser: User = {
          ...userData,
          id: Date.now().toString(),
          createdAt: new Date().toISOString(),
          updatedAt: new Date().toISOString(),
        };
        mockUsers.push(newUser);
        resolve({ success: true, user: newUser });
      }, 500);
    });
  },

  // 更新用户
  updateUser: (id: string, userData: Partial<User>) => {
    return new Promise<{ success: boolean; user?: User; message?: string }>((resolve) => {
      setTimeout(() => {
        const index = mockUsers.findIndex(user => user.id === id);
        if (index !== -1) {
          mockUsers[index] = {
            ...mockUsers[index],
            ...userData,
            updatedAt: new Date().toISOString(),
          };
          resolve({ success: true, user: mockUsers[index] });
        } else {
          resolve({ success: false, message: '用户不存在' });
        }
      }, 500);
    });
  },

  // 删除用户
  deleteUser: (id: string) => {
    return new Promise<{ success: boolean; message?: string }>((resolve) => {
      setTimeout(() => {
        const index = mockUsers.findIndex(user => user.id === id);
        if (index !== -1) {
          mockUsers.splice(index, 1);
          resolve({ success: true });
        } else {
          resolve({ success: false, message: '用户不存在' });
        }
      }, 500);
    });
  },
}; 